trigger DNC_IDNC_maskingEmailonAccount on Account (before insert,before update) {

    Boolean UserPermission = False;

    // Below code if Profile Ids are used
    Set<String> ProfileIds = new Set<String>();
    for (UserAccessDNC__c UA_DNC : UserAccessDNC__c.getAll().values()){
        ProfileIds.add(UA_DNC.Profile_Ids__c.substring(0, 15));
    } 
    if(!ProfileIds.isEmpty()){
        UserPermission = ProfileIds.contains(UserInfo.getProfileId().substring(0, 15));
    }          
/*    Schema.DescribeSObjectResult d = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName =  rtMapByName.get('West - Student Account');
    String RecTypeId  = rtByName.getRecordTypeId();*/

    for(Account A: Trigger.New){
    if(trigger.isUpdate ){
        
        //  Personal Email Masking:
            if(Trigger.oldMap.get(A.Id).Personal_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Personal_Email_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Personal_Email__pc != Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).Personal_Email_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Personal_Email_Status__pc == 'DNC') {
                A.Personal_Email__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Personal_Email_Status__pc  == 'DNC' && Trigger.newMap.get(A.Id).Personal_Email_Status__pc  != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Personal_Email__pc = A.Personal_Email_Unmasked__pc;
                }                
            } else {
                A.Personal_Email_Unmasked__pc= A.Personal_Email__pc ;
            }        

        //  University Email Masking:
            if(Trigger.oldMap.get(A.Id).University_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).University_Email_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).University_Email__pc != Null){ A.addError('You cannot be changed "Work Email" once it\'s status is marked as Opt Out!');}                          
            } else if(Trigger.oldMap.get(A.Id).University_Email_Status__pc != 'DNC' &&  Trigger.newMap.get(A.Id).University_Email_Status__pc == 'DNC') {
                A.University_Email__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).University_Email_Status__pc == 'DNC' &&  Trigger.newMap.get(A.Id).University_Email_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.University_Email__pc = A.University_Email_Unmasked__pc;
                }                
            } else  {
                A.University_Email_Unmasked__pc= A.University_Email__pc;
            }        

        //  Mother Email Masking:
            if(Trigger.oldMap.get(A.Id).Mother_s_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Email_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Mother_s_Email__pc != Null){A.addError('You cannot be changed "Mother\'s Email" once it\'s status is marked as Opt Out!');}                          
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Email_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Email_Status__pc == 'DNC') {
                A.Mother_s_Email__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Email_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Mother_s_Email__pc = A.Mother_s_Email_Unmasked__pc;
                }                
            } else {
                A.Mother_s_Email_Unmasked__pc= A.Mother_s_Email__pc;
            }        

        //  Father Email Masking:
            if(Trigger.oldMap.get(A.Id).Father_s_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Email_Status__pc == 'DNC'){
                IF(Trigger.newMap.get(A.Id).Father_s_Email__pc != Null){A.addError('You cannot be changed "Father\'s Email" once it\'s status is marked as opt Out!');}                          
            } else if(Trigger.oldMap.get(A.Id).Father_s_Email_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Father_s_Email_Status__pc == 'DNC') {
                A.Father_s_Email__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Father_s_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Email_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with opt Out permissions can change "opt Out" once it\'s status is marked as opt Out!');
                } else {
                    A.Father_s_Email__pc = A.Father_s_Email_Unmasked__pc;
                }                
            } else {
                A.Father_s_Email_Unmasked__pc= A.Father_s_Email__pc;
            }        

        //  Marketing Email Masking:
            if(Trigger.oldMap.get(A.Id).Marketing_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Email_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Marketing_Email__pc != Null){A.addError('You cannot be changed "Marketing Email" once it\'s status is marked as opt Out!');}                          
            } else if(Trigger.oldMap.get(A.Id).Marketing_Email_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Marketing_Email_Status__pc == 'DNC') {
                A.Marketing_Email__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Marketing_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Email_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with opt Out permissions can change "opt Out" once it\'s status is marked as opt Out!');
                } else {
                    A.Marketing_Email__pc = A.Marketing_Email_Unmasked__pc;
                }                
            } else {
                A.Marketing_Email_Unmasked__pc= A.Marketing_Email__pc;
            }        

        //  Other Email Masking:
            if(Trigger.oldMap.get(A.Id).Other_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Other_Email__pc != Null){A.addError('You cannot be changed "Other Email" once it\'s status is marked as opt Out!');}                          
            } else if(Trigger.oldMap.get(A.Id).Other_Email_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Other_Email_Status__pc == 'DNC') {
                A.Other_Email__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with opt Out permissions can change "opt Out" once it\'s status is marked as opt Out!');
                } else {
                    A.Other_Email__pc = A.Other_Email_Unmasked__pc;
                }                
            } else {
                A.Other_Email_Unmasked__pc= A.Other_Email__pc;
            }        
        
        //  Other Email 2 Masking:
            if(Trigger.oldMap.get(A.Id).Other_Email_2_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_2_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Other_Email_2__pc != Null){A.addError('You cannot be changed "Other Email 2" once it\'s status is marked as opt Out!');}                          
            } else if(Trigger.oldMap.get(A.Id).Other_Email_2_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Other_Email_2_Status__pc == 'DNC') {
                A.Other_Email_2__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Email_2_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_2_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with opt Out permissions can change "opt Out" once it\'s status is marked as opt Out!');
                } else {
                    A.Other_Email_2__pc = A.Other_Email_2_Unmasked__pc;
                }                
            } else {
                A.Other_Email_2_Unmasked__pc= A.Other_Email_2__pc;
            }        

        //  Work Email Masking:
            if(Trigger.oldMap.get(A.Id).Work_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Work_Email_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Work_Email__pc != Null){A.addError('You cannot be changed "Work Email" once it\'s status is marked as opt Out!');}                          
            } else if(Trigger.oldMap.get(A.Id).Work_Email_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Work_Email_Status__pc == 'DNC') {
                A.Work_Email__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Work_Email_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Work_Email_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with opt Out permissions can change "opt Out" once it\'s status is marked as opt Out!');
                } else {
                    A.Work_Email__pc = A.Work_Email_Unmasked__pc;
                }                
            } else {
                A.Work_Email_Unmasked__pc= A.Work_Email__pc;
            }        
    }
    else if(trigger.isInsert ){

        //  Personal Email Masking:
            if(A.Personal_Email_Status__pc == 'DNC') {
                    A.Personal_Email_Unmasked__pc= A.Personal_Email__pc;
                    A.Personal_Email__pc='';                           
            } else {
                A.Personal_Email_Unmasked__pc= A.Personal_Email__pc;
            }        
        
        //  University Email Masking:
            if(A.University_Email_Status__pc == 'DNC') {
                    A.University_Email_Unmasked__pc= A.University_Email__pc;
                    A.University_Email__pc='';                           
            } else {
                A.University_Email_Unmasked__pc= A.University_Email__pc;
            }        
        
        //  Mother Email Masking:
            if(A.Mother_s_Email_Status__pc == 'DNC') {
                    A.Mother_s_Email_Unmasked__pc= A.Mother_s_Email__pc;
                    A.Mother_s_Email__pc='';                           
            } else {
                A.Mother_s_Email_Unmasked__pc= A.Mother_s_Email__pc;
            }        
        
        //  Father Email Masking:
            if(A.Father_s_Email_Status__pc == 'DNC') {
                    A.Father_s_Email_Unmasked__pc= A.Father_s_Email__pc;
                    A.Father_s_Email__pc='';                           
            } else {
                A.Father_s_Email_Unmasked__pc= A.Father_s_Email__pc;
            }        
        
        //  Marketing Email Masking:
            if(A.Marketing_Email_Status__pc == 'DNC')  {
                    A.Marketing_Email_Unmasked__pc= A.Marketing_Email__pc;
                    A.Marketing_Email__pc='';                           
            } else {
                A.Marketing_Email_Unmasked__pc= A.Marketing_Email__pc;
            }        
        
        //  Other Email Masking:
            if(A.Other_Email_Status__pc == 'DNC') {
                    A.Other_Email_Unmasked__pc= A.Other_Email__pc;
                    A.Other_Email__pc='';                           
            } else {
                A.Other_Email_Unmasked__pc= A.Other_Email__pc;
            }        
        
        //  Other Email 2 Masking:
            if(A.Other_Email_2_Status__pc == 'DNC') {
                    A.Other_Email_2_Unmasked__pc= A.Other_Email_2__pc;
                    A.Other_Email_2__pc='';                           
            } else {
                A.Other_Email_2_Unmasked__pc= A.Other_Email_2__pc;
            }        
        
        //  Work Email Masking:
            if(A.Work_Email_Status__pc == 'DNC') {
                    A.Work_Email_Unmasked__pc= A.Work_Email__pc;
                    A.Work_Email__pc='';                           
            } else {
                A.Work_Email_Unmasked__pc= A.Work_Email__pc;
            }        
    }
    }
}